<template>
  <div class="page-container">
    <button class="back-button" @click="goToHome">
      <span class="icon-arrow-left"></span>
      返回首页
    </button>
    
    <div class="header-actions">
      <h1>设备台账</h1>
      <button class="primary-btn" @click="showAddDeviceModal = true">
        <span class="icon-plus"></span> 新增设备
      </button>
    </div>
    
    <p>这里是设备台账页面，您可以查看和管理所有设备信息。</p>
    
    <div class="card">
      <div class="search-filter">
        <input 
          type="text" 
          v-model="searchQuery" 
          placeholder="搜索设备名称或编号..." 
          class="search-input"
        >
        <select v-model="filterStatus" class="filter-select">
          <option value="">所有状态</option>
          <option value="正常">正常</option>
          <option value="维修中">维修中</option>
          <option value="已报废">已报废</option>
        </select>
      </div>
      
      <table class="device-table">
        <thead>
          <tr>
            <th>设备编号</th>
            <th>设备名称</th>
            <th>型号规格</th>
            <th>购置日期</th>
            <th>状态</th>
            <th>负责人</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="device in filteredDevices" :key="device.id" class="table-row">
            <td>{{ device.code }}</td>
            <td>{{ device.name }}</td>
            <td>{{ device.model }}</td>
            <td>{{ device.purchaseDate }}</td>
            <td>
              <span :class="`status-badge ${device.statusClass}`">
                {{ device.status }}
              </span>
            </td>
            <td>{{ device.manager }}</td>
            <td class="action-buttons">
              <button class="btn-view" @click="viewDevice(device)">查看</button>
              <button class="btn-edit" @click="editDevice(device)">编辑</button>
            </td>
          </tr>
          <tr v-if="filteredDevices.length === 0">
            <td colspan="7" class="no-data">暂无设备数据</td>
          </tr>
        </tbody>
      </table>
      
      <div class="pagination">
        <button 
          class="page-btn" 
          :disabled="currentPage === 1" 
          @click="currentPage--"
        >
          上一页
        </button>
        <span class="page-info">
          第 {{ currentPage }} 页，共 {{ totalPages }} 页
        </span>
        <button 
          class="page-btn" 
          :disabled="currentPage === totalPages" 
          @click="currentPage++"
        >
          下一页
        </button>
      </div>
    </div>
    
    <!-- 新增/编辑设备弹窗 -->
    <div v-if="showAddDeviceModal" class="modal-overlay">
      <div class="modal">
        <div class="modal-header">
          <h3>{{ isEditing ? '编辑设备' : '新增设备' }}</h3>
          <button class="close-btn" @click="showAddDeviceModal = false">&times;</button>
        </div>
        <div class="modal-body">
          <form class="device-form">
            <div class="form-group">
              <label>设备编号</label>
              <input type="text" v-model="currentDevice.code" required>
            </div>
            <div class="form-group">
              <label>设备名称</label>
              <input type="text" v-model="currentDevice.name" required>
            </div>
            <div class="form-group">
              <label>型号规格</label>
              <input type="text" v-model="currentDevice.model">
            </div>
            <div class="form-group">
              <label>购置日期</label>
              <input type="date" v-model="currentDevice.purchaseDate">
            </div>
            <div class="form-group">
              <label>状态</label>
              <select v-model="currentDevice.status">
                <option value="正常">正常</option>
                <option value="维修中">维修中</option>
                <option value="已报废">已报废</option>
              </select>
            </div>
            <div class="form-group">
              <label>负责人</label>
              <input type="text" v-model="currentDevice.manager">
            </div>
          </form>
        </div>
        <div class="modal-footer">
          <button class="cancel-btn" @click="showAddDeviceModal = false">取消</button>
          <button class="save-btn" @click="saveDevice">保存</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'DeviceInventory',
  data() {
    return {
      searchQuery: '',
      filterStatus: '',
      currentPage: 1,
      pageSize: 10,
      showAddDeviceModal: false,
      isEditing: false,
      currentDevice: {},
      devices: [
        { id: 1, code: 'DEV-001', name: '笔记本电脑', model: 'ThinkPad X1', purchaseDate: '2022-01-15', status: '正常', statusClass: 'status-normal', manager: '张三' },
        { id: 2, code: 'DEV-002', name: '打印机', model: 'HP LaserJet Pro', purchaseDate: '2021-05-20', status: '维修中', statusClass: 'status-maintenance', manager: '李四' },
        { id: 3, code: 'DEV-003', name: '投影仪', model: 'Epson CB-X06', purchaseDate: '2020-09-10', status: '正常', statusClass: 'status-normal', manager: '王五' },
        { id: 4, code: 'DEV-004', name: '扫描仪', model: 'Canon DR-C225', purchaseDate: '2022-03-05', status: '已报废', statusClass: 'status-scrapped', manager: '赵六' },
      ]
    };
  },
  computed: {
    filteredDevices() {
      return this.devices
        .filter(device => 
          (this.searchQuery === '' || 
           device.name.includes(this.searchQuery) || 
           device.code.includes(this.searchQuery)) &&
          (this.filterStatus === '' || device.status === this.filterStatus)
        )
        .slice(
          (this.currentPage - 1) * this.pageSize,
          this.currentPage * this.pageSize
        );
    },
    totalPages() {
      const filteredCount = this.devices.filter(device => 
        (this.searchQuery === '' || 
         device.name.includes(this.searchQuery) || 
         device.code.includes(this.searchQuery)) &&
        (this.filterStatus === '' || device.status === this.filterStatus)
      ).length;
      return Math.ceil(filteredCount / this.pageSize);
    }
  },
  methods: {
    goToHome() {
      this.$router.push('/home');
    },
    viewDevice(device) {
      // 查看设备详情逻辑
      console.log('查看设备:', device);
    },
    editDevice(device) {
      this.isEditing = true;
      this.currentDevice = { ...device };
      this.showAddDeviceModal = true;
    },
    saveDevice() {
      if (this.isEditing) {
        const index = this.devices.findIndex(d => d.id === this.currentDevice.id);
        if (index !== -1) {
          this.devices[index] = { ...this.currentDevice };
          // 更新状态样式类
          this.devices[index].statusClass = this.getStatusClass(this.currentDevice.status);
        }
      } else {
        // 新增设备
        const newDevice = {
          ...this.currentDevice,
          id: this.devices.length + 1,
          statusClass: this.getStatusClass(this.currentDevice.status)
        };
        this.devices.unshift(newDevice);
      }
      this.showAddDeviceModal = false;
      this.isEditing = false;
      this.currentDevice = {};
    },
    getStatusClass(status) {
      switch(status) {
        case '正常': return 'status-normal';
        case '维修中': return 'status-maintenance';
        case '已报废': return 'status-scrapped';
        default: return '';
      }
    }
  }
};
</script>

<style scoped>
.page-container {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

.header-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 20px 0;
}

.primary-btn {
  background: #2ecc71;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 6px;
  transition: background 0.3s;
}

.primary-btn:hover {
  background: #27ae60;
}

.card {
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.1);
  padding: 20px;
  margin-bottom: 20px;
}

.search-filter {
  display: flex;
  gap: 15px;
  margin-bottom: 20px;
}

.search-input, .filter-select {
  padding: 8px 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  flex: 1;
  max-width: 300px;
}

.device-table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 20px;
}

.device-table th, .device-table td {
  padding: 12px 15px;
  text-align: left;
  border-bottom: 1px solid #eee;
}

.device-table th {
  background: #f8f9fa;
  font-weight: 600;
}

.table-row:hover {
  background: #f9f9f9;
  transition: background 0.2s;
}

.status-badge {
  padding: 3px 8px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
}

.status-normal {
  background: #eafaf1;
  color: #27ae60;
}

.status-maintenance {
  background: #fef5e7;
  color: #f39c12;
}

.status-scrapped {
  background: #fdedeb;
  color: #e74c3c;
}

.action-buttons {
  display: flex;
  gap: 8px;
}

.btn-view, .btn-edit {
  padding: 4px 8px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 13px;
}

.btn-view {
  background: #e3f2fd;
  color: #1976d2;
}

.btn-edit {
  background: #fff3e0;
  color: #e65100;
}

.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 15px;
  margin-top: 20px;
}

.page-btn {
  padding: 6px 12px;
  border: 1px solid #ddd;
  background: white;
  border-radius: 4px;
  cursor: pointer;
}

.page-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.page-info {
  color: #666;
}

.no-data {
  text-align: center;
  padding: 40px;
  color: #999;
}

/* 弹窗样式 */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0,0,0,0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.modal {
  background: white;
  border-radius: 8px;
  width: 500px;
  max-width: 90%;
  box-shadow: 0 5px 15px rgba(0,0,0,0.2);
}

.modal-header {
  padding: 15px 20px;
  border-bottom: 1px solid #eee;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.close-btn {
  background: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
  color: #999;
}

.modal-body {
  padding: 20px;
}

.device-form {
  display: grid;
  gap: 15px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.form-group label {
  font-weight: 500;
  font-size: 14px;
}

.form-group input, .form-group select {
  padding: 8px 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
}

.modal-footer {
  padding: 15px 20px;
  border-top: 1px solid #eee;
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

.cancel-btn, .save-btn {
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
  border: 1px solid #ddd;
}

.save-btn {
  background: #3498db;
  color: white;
  border-color: #3498db;
}

.cancel-btn:hover {
  background: #f5f5f5;
}

.save-btn:hover {
  background: #2980b9;
}

/* 继承并复用返回按钮样式 */
.back-button {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 15px;
  background: #3498db;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  transition: all 0.3s;
  margin-right: 20px;
  font-weight: 500;
}

.back-button:hover {
  background: #2980b9;
  transform: translateY(-2px);
}
</style>